/*
 * Created on Mar 23, 2006
 *
 */
package org.mushroomdb.triggers;

import org.mushroomdb.catalog.Table;
import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.query.Query;
import org.mushroomdb.service.ServiceManager;
import org.mushroomdb.storedprocedures.StoredProcedure;

/**
 * CREATE TRIGGER <trigger name> <BEFORE|AFTER> <INSERT|DELETE|UPDATE>
 * ON <relation name> FOR EACH <ROW|STATEMENT>
 * EXECUTE [ QUERY <SQL query> | STATEMENT <statement (args)> ];
 * 
 * @author mchiodi
 */
public class CreateTriggerQuery extends Query {
	private Trigger trigger;
	private Table table;

	/**
	 * Constructs a CreateTriggerQuery for a Query Trigger.
	 */
	public CreateTriggerQuery(String name, int type, int operation, Table table, int mode, Query query) {
		super();
		this.trigger = new Trigger(name,operation,type,mode,query);
		this.table = table;
	}
	
	/**
	 * Constructs a CreateTriggerQuery for a StoredProcedure Trigger.
	 */
	public CreateTriggerQuery(String name, int type, int operation, Table table, int mode, StoredProcedure storedProcedure) {
		super();
		this.trigger = new Trigger(name,operation,type,mode,storedProcedure);
		this.table = table;
	}
	
	/**
	 * @see org.mushroomdb.query.Query#execute(org.mushroomdb.engine.EvaluationEngine)
	 */
	public Object execute(EvaluationEngine evaluationEngine) {
		TriggersService triggersService = (TriggersService) ServiceManager.getInstance().getService(TriggersService.class);
		triggersService.addTrigger(trigger,table);
		return Boolean.TRUE;
	}

}
